//引入数据模型层
const adminModel = require('../models/adminModel');
const utils = require('../utils/index');
const jwt = require('jsonwebtoken'); //用来生成token

exports.index = (req, res) => {
    adminModel.getNewArtical().then(function(data) {
        data = utils.arrFormat(data);
        res.json({
            data: data
        });
    })
}

let secretOrPrivateKey = "cxblog"; //加密的key
exports.login = (req, res) => {
    //去数据库比对
    adminModel.login(req.body).then(function(data) {
        if (data.length > 0) {
            //生成token
            let token = jwt.sign({
                user: data[0]
            }, secretOrPrivateKey, {
                expiresIn: 60 * 60 * 1  // 一小时过期
            })
            res.json({
                code: 200,
                msg: '登录成功',
                token: token,
                user: data[0]
            })
        } else {
            //登录失败--带一个错误信息回去
            res.json({
                code: 100,
                msg: '登录失败',
            })
        }
    })
}
exports.login1 = (req, res) => {
    //去数据库比对
    adminModel.login1(req.body).then(function(data) {
        if (data.length > 0) {
            //生成token
            let token = jwt.sign({
                user: data[0]
            }, secretOrPrivateKey, {
                expiresIn: 60*60*4 // 一小时过期
            })
            res.json({
                code: 200,
                msg: '登录成功',
                token: token,
                user: data[0]
            })
        } else {
            //登录失败--带一个错误信息回去
            res.json({
                code: 100,
                msg: '登录失败',
            })
        }
    })
}

//查询所有代理数据
exports.getAgents = (req, res) => {
    adminModel.getAllAgents().then(function(data) {
        if (data.length > 0) {
            data = utils.arrFormat(data);
            res.json({
                code: 200,
                msg: '获取成功',
                data: data
            })
        }else{
            res.json({
                code: 206,
                msg: '没有数据'
                
            })
        }

    })
}

//获取代理信息
exports.getOneAgent = (req, res) => {
        adminModel.getOneAgent(req.params.tel).then(function(data) {
            console.log(data);
            if (data.length > 0) {
                data = utils.arrFormat(data);
                res.json({
                    code: 200,
                    msg: '查询成功',
                    data: data
                });
            } else {
                res.json({
                    code: 206,
                    msg: '没有数据',
                });
            }
        })

    }
    //添加代理接口
exports.addAgent = (req, res) => {
    console.log(req.body);
        adminModel.addAgent(req.body).then(function(data) {
            if (data.affectedRows > 0) {
                res.json({
                    code: 200,
                    msg: '添加代理成功'
                })
            }
        })

    }
    //编辑代理接口
exports.updateAgent = (req, res) => {
    adminModel.editAgent(req.body).then(function(data) {
        console.log(data)
        if (data.length > 0 && data[0].affectedRows > 0 && data[1].affectedRows > 0) {
            res.json({
                code: 200,
                msg: '编辑成功',
            })
        }
    })
}

// 查询玩家基础数据
exports.getPlayerInfo = (req, res) => {
    adminModel.getPlayerInfo(req.params.uid).then(result => {
        console.log(result)
        if (result.code == 200) {
            res.json({
                code: 200,
                data: result.data
            })
        } else {
            res.json({
                code: 206,
                data: '获取失败'
            })
        }
    });
};
// 查询用户反馈数据列表
exports.getFeedBackInfo = (req, res) => {
    adminModel.getFeedBacks().then(result => {
        console.log(result);
        if (result.code == 200) {
            res.json({
                code: 200,
                data: result.data
            })
        } else {
            res.json({
                code: 206,
                data: '获取失败'
            })
        }
    });
};
// 查询游戏大厅数据
exports.getGameHallInfo = (req, res) => {
    adminModel.getGameHallInfo().then(result => {
        console.log(result);
        if (result.code == 200) {
            res.json({
                code: 200,
                data: result.data
            })
        } else {
            res.json({
                code: 206,
                data: '获取失败'
            })
        }
    });
};
// 修改游戏大厅信息
exports.UpdateGameHallInfo = (req, res) => {
    console.log(req.body)
    adminModel.UpdateGameHallInfo(req.body).then(result => {
        console.log(result);
        if (result.code == 200) {
            res.json({
                code: 200,
                // data: result.data
            })
        } else {
            res.json({
                code: 206,
                data: '获取失败'
            })
        }
    });
};
exports.changeTeaHouse = (req,res)=>{
    console.log(req.body)
    adminModel.changeTeaHouse(req.body).then(result=>{
        console.log(result)
        if(result.code ==200){
            //修改成功
            res.json({
                code: 200,
                data: '修改成功'
            })
        }else{
            //修改失败 提示参数错误
            res.json({
                code: 206,
                data: '修改失败'
            })
        }
    });
}

//给玩家充值房卡接口
exports.addRoomCardForPlayer = (req, res) => {
    // 加减玩家房卡
    adminModel.addRoomCard(req.body).then(result => {
        if (result.code && result.code == 200) {
            //生成一条充卡记录
            let record = {
                agentId: req.body.agentId,
                uid: req.body.uid,
                addCardNumber: req.body.addNumber
            }
            adminModel.addCardRecord(record).then(data => {
                console.log(data);
            })
            res.json({
                code: 200,
                msg: '充值成功',
            })
        } else {
            res.json({
                code: 204,
                msg: '充值失败',
            })
        }
    });
};
exports.addRoomCardForAgent = function(req, res) {
    adminModel.addCardForAgentRecord(req.body).then(data => {
        console.log(res)
        if (data.length > 0 && data[0].affectedRows > 0 && data[1].affectedRows > 0 && data[2].affectedRows > 0) {
            res.json({
                code: 200,
                msg: '充值成功',
            })
        } else {
            res.json({
                code: 204,
                msg: '充值失败',
            })
        }
    });

}
exports.getRecordById = (req, res) => {
    adminModel.getRecordById(req.params.agentId).then(result => {
        if (result.length > 0) {
            result = utils.timeFormat(result);
            res.json({
                code: 200,
                data: result
            })
        } else {
            res.json({
                code: 204,
                msg: '未查询到充值记录',
            })
        }
    });

};
exports.getRecordByManagerId = (req, res) => {
    adminModel.getRecordByManagerId(req.params.managerId).then(result => {
        if (result.length > 0) {
            result = utils.timeFormat1(result);
            res.json({
                code: 200,
                data: result
            })
        } else {
            res.json({
                code: 204,
                msg: '未查询到充值记录',
            })
        }
    });

}